package cn.zijian.wechat.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import cn.zijian.models.Message;
import cn.zijian.wechat.R;
import cn.zijian.wechat.activities.ChatActivity;
import cn.zijian.wechat.adapter.MessageAdapter;

/**
 * Created by Administrator on 2017/12/13.
 */

public class MessageFragment extends Fragment
{
    private ListView listView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_message,container, false);
        listView = (ListView) view.findViewById(R.id.lvMessage);

        MessageAdapter adapter = new MessageAdapter(getContext(), R.layout.layout_listview_message, initMessages());
        listView.setAdapter(adapter);
        
        listenListViewItemClick();
        return view;
    }

    /**
     * 监听listView点击事件
     */
    private void listenListViewItemClick()
    {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
                {
                    Message message = initMessages().get(i);
                    Intent intent = new Intent(getContext(), ChatActivity.class);
                    intent.putExtra("toUserID", message.getUserID());
                    startActivity(intent);
                    //Toast.makeText(getContext(),message.getUserID(),Toast.LENGTH_SHORT).show();
                }
            }
        );
    }

    /**
     * 初始化消息数据
     * @return
     */
    private ArrayList<Message> initMessages()
    {
        return new ArrayList<Message>(){{
            add(new Message("子建",R.drawable.avatar_01,"USER_01","2017/12/13","message_01"));
            add(new Message("TEST_ACCOUNT_02",R.drawable.avatar_01,"USER_02","2017/12/13","message_02"));
            add(new Message("TEST_ACCOUNT_03",R.drawable.avatar_01,"USER_03","2017/12/13","message_03"));
            add(new Message("TEST_ACCOUNT_04",R.drawable.avatar_01,"USER_04","2017/12/13","message_04"));
            add(new Message("TEST_ACCOUNT_05",R.drawable.avatar_01,"USER_05","2017/12/13","message_05"));
            add(new Message("TEST_ACCOUNT_06",R.drawable.avatar_01,"USER_06","2017/12/13","message_06"));
            add(new Message("TEST_ACCOUNT_07",R.drawable.avatar_01,"USER_07","2017/12/13","message_07"));
            add(new Message("TEST_ACCOUNT_08",R.drawable.avatar_01,"USER_08","2017/12/13","message_08"));
            add(new Message("TEST_ACCOUNT_09",R.drawable.avatar_01,"USER_09","2017/12/13","message_09"));
        }};
    }
}
